# usage: add-teams-to-db.py input_file input_stad input_nick input_fullname db_file

import sys, os, re

def CreateSynList(synList, fullName):
    synListOut = ""
    synList.append(fullName)
    for item in synList:
        if (item != ""):
            synListOut += "\"%s\" " % item
        
    return synListOut

def ExtractValue(line):
    line = line.strip("\n")
    line = re.sub("\s*{{[^}]+}}\s*", "\t", line)
    line = re.sub("«", "", line)
    line = re.sub("»", "", line)
    #line = re.sub("\s*\([^\)]+\)\s*", "\t", line)
    line = re.sub("''", "", line)
    line = re.sub("\t*\|.*$", "", line)
    #line = re.sub("\|", "", line)
    #fields = line.split("\t")
    #fields = [x for x in fields if x]
    #if (nField < len(fields)):
        #return fields[nField]
    return line.split("\t")[-1:][0]

if (len(sys.argv) < 3):
    print ("использование: add-teams-to-db.py input_file db_file")
    sys.exit(1)

i = 0
with open (sys.argv[1]) as team_name:
    with open (sys.argv[2]) as team_syn:
        with open(sys.argv[3]) as team_fullname:
            for line in team_name:
                line_nicks = team_syn.readline()
                line_fullname = team_fullname.readline()
                
                teamName = ExtractValue(line)
                teamSynonyms = ExtractValue(line_nicks)
                teamFullname = ExtractValue(line_fullname)
                
                if (teamName == ""):
                    teamName = line[(len(line.split("\t")[0]) + 1):line.find(" (")]
                    
                if (teamSynonyms.isdigit()):
                    teamSynonyms = ""
                    
                teamName = teamName.replace("[[", "")
                teamName = teamName.replace("]]", "")
                teamName = teamName.replace("&lt;br /&gt;", "")
                
                teamSynonyms = teamSynonyms.replace(" (", ", ")
                teamSynonyms = teamSynonyms.replace(")", "")
                teamSynonyms = teamSynonyms.replace("[[", "")
                teamSynonyms = teamSynonyms.replace("]]", "")
                teamSynonyms = teamSynonyms.replace("''", "")
                teamSynonyms = teamSynonyms.replace("&lt;br /&gt;", ", ")
                city = "null"
                
                if (teamName.find(" (") != -1):
                    (teamName, city) = teamName[:-1].split(" (")
                
                teamFullname = teamFullname.replace("(", "")
                teamFullname = teamFullname.replace(")", "")
                teamFullname = teamFullname.replace("[[", "")
                teamFullname = teamFullname.replace("]]", "")
                teamFullName = teamFullname.replace("&lt;br /&gt;", "")
                    
                
                #print ("TeamName: %s\nCity: %s\nSynonyms: %s\nTeamFullName: %s\n\n" % (teamName, city, teamSynonyms, teamFullname))
                
                os.system("add-team.py \"%s\" 1 \"%s\" %s \"%s\"" % (teamName, city, "\"" + teamSynonyms.replace(", ", "\" \"") + "\"", teamFullname))
                
                #if (i > 1):
                #    break
                #i+=1